package service.impl;

import common.consts.Consts;
import dao.AccountDao;
import dao.EducationDao;
import domain.Account;
import domain.Education;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import service.EducationService;
import service.ServiceResponse;

/**
 * Created by zf on 2015/7/13.
 */

@Service
public class EducationServiceImpl extends ServiceBase implements EducationService {


    @Autowired
    private AccountDao accountDao;

    @Autowired
    private EducationDao educationDao;

    @Override
    public ServiceResponse saveEducationInfo(long accountId, String school, String major, String grade) {

        Account account = accountDao.get(accountId);

        if (account == null) {
            return ServiceResponse.fail(Consts.i18nKey.INVALID_USER_ID);

        }

        Education education = educationDao.get(accountId);
        if (education == null) {
            //新建一条education信息
            education = new Education();
            education.setId(accountId);
            education.setSchool(school == null ? "" : school);
            education.setMajor(major == null ? "" : major);
            education.setGrade(grade == null ? "" : grade);
            educationDao.insert(education);
        }

        if (education != null) {
            //更新已有的education信息
            education.setSchool(school == null ? "" : school);
            education.setMajor(major == null ? "" : major);
            education.setGrade(grade == null ? "" : grade);
            educationDao.update(education);

        }


        return ServiceResponse.success();
    }


    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    public void setEducationDao(EducationDao educationDao) {
        this.educationDao = educationDao;
    }
}
